package com.gzone.contentprovider.adapter;

import java.util.ArrayList;

import com.gzone.contentprovider.R;
import com.gzone.contentprovider.helper.BitmapHelper;
import com.gzone.contentprovider.sql.model.Item;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemListAdapter extends BaseAdapter{
	
	private Context context;
	private ArrayList<Item> items;

	LayoutInflater inflater;
	public ItemListAdapter(Context context, ArrayList<Item> items){
		this.context = context;
		this.items = items;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return items.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parentView) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item_list_row, null);
			holder = new Holder();
			holder.title = (TextView) convertView.findViewById(R.id.textview_item_title);
			holder.price = (TextView) convertView.findViewById(R.id.textview_item_price);
			holder.icon = (ImageView) convertView.findViewById(R.id.imageView_item_icon);
			convertView.setTag(holder);
		}else{
			holder = (Holder) convertView.getTag();
		}
		holder.title.setText(items.get(position).title);
		holder.price.setText(items.get(position).price);
		if(items.get(position).icon != null)
			holder.icon.setImageBitmap(BitmapHelper.convertByteToBitmap(items.get(position).icon));
		return convertView;
	}
	
	class Holder{
		TextView title;
		TextView price;
		ImageView icon;
	}

}
